package cn.tedu.cs.controller;

import cn.tedu.cs.Mapper.UserMapper;
import cn.tedu.cs.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class UserController {
    @Autowired(required = false)
    UserMapper mapper;

    @RequestMapping("/login")
    public int login(@RequestBody User user){
        System.out.println("User = " +user);
        User u = mapper.selectByUsername(user.getUsername());
        if(u==null)return 2;
        if(u.getPassword().equals(user.getPassword()))return 1;
        return 3;
    }

    @RequestMapping("/currentUser")
    public User currentUser(HttpSession session){
        User user=(User)session.getAttribute("user");
        return user;
    }

    @RequestMapping("/logout")
    public void logout(HttpSession session){
        session.removeAttribute("user");
    }

}
